/**
 *  Description: this file define the WinManager class and some static class method
 *  Copyright:  2002-2005
 *  Author: 赖毅
 *  Last Modified: 2003-03-02
 *  Documented by Yee Sun
 */
/**
 *  constructor
 */
function WinManager() {}

/**
 *  Open a simple window with a width and height
 *  the window have only window title bar, without menu, toolbar, location bar and status bar
 *  @param url the window's url
 *  @param params the parameter array to be encoded
 *  @param width the width of the open window, default is 120
 *  @param height the height of the open window, default is 80
 *  @param scroll whether has scroll bar default is yes
 *  @param menubar whether has menubar default is none
 *  @param sLocation the location of the open window, default is _blank
 *  @return the opened window object
 */
WinManager.openSimple = function (URL,params,width,height,scroll,bMenubar,sLocation,position) {
    if (width == null) width = 120;
    if (height == null) height = 80;
    scroll= (scroll)? "yes":"no";
    bMenubar = (bMenubar)? "1":"0";
    var option = "height="+height+",left="+(screen.availWidth-width)/2+",status=no,menubar="+bMenubar
            +"toolbar=no,menubar=no,resizable=no,location=no,status=no,width="+width
            +",scrollbars = "+scroll+"";
    if (position.left) {
    	option = option + ",left=" + position.left;
    }
    if (position.top) {
    	option = option + ",top=" + position.top;
    } else {
    	option = option + ",top="+(screen.availHeight-height)/2
    }
    //alert(__WD_encodeParams(URL,params));
    var opened =  window.open(WinManager.encodeParams(URL,params),(sLocation==null)?"_blank":sLocation,option);
//    if (opened != null && opened.opener!=this) {
//        this.opener = opened.opener;
//        opened.opener = this;
//    }
    return opened;
}

/**
 *  Open an window with a width and height
 *  @param url the window's url
 *  @param params the parameter array to be encoded
 *  @param width
 *  @param height
 *  @return the opened window object
 */
WinManager.open = function (URL,params,width,height) {
    var option = "status=1,menubar=1,toolbar=0,fullscreen=0,location=1,directories=1";
    if (width != null) option += ",width="+width+",left="+(screen.availWidth-width)/2;
    if (height != null) option += ",height="+height+",top="+(screen.availHeight-height)/2;
    option += ",scrollbars = yes"+",resizable=1";
    var opened =  window.open(WinManager.encodeParams(URL,params),"_blank",option);
    if (opened.opener!=this) {
        this.opener = opened.opener;
        opened.opener = this;
    }
    return opened;
}

WinManager.help = function (URL,params) {
    var option = "help:no;resizable:no;scroll:no;status:no;dialogWidth:200px;dialogHeight:200px";
    window.showModelessDialog(URL,params,option);
}

/**
 *  show an modal dialog and return the return value
 *  @param url the dialog's url
 *  @param param the parameter to be passed
 *  @param width default = 120
 *  @param height default = 80
 *  @param scroll default = no
 *  @return the opened window object
 */
WinManager.dialog = function (url,param,width,height,scroll,left,top) {
    var sFeatures = " edge: sunken ; center: Yes; help: No; resizable: No; status: No;";
    if (width == null) width = 120;
    if (height == null) height = 80;

    sFeatures += "dialogWidth:"+width+"px ; ";
    sFeatures += "dialogHeight:"+height+"px ; ";

    if (scroll !=null)
        sFeatures+= "scroll:yes;";
    else
        sFeatures+= "scroll:no;";
    if(left!=null){
        sFeatures+= "dialogLeft:"+left+"px;";
    }
    if(top!=null){
        sFeatures+= "dialogTop:"+top+"px;";
    }
    return window.showModalDialog(url,param==null?null:param,sFeatures);
}

/**
 *  resize and center the window
 *  @param iWidth the width to be set
 *  @param iHeight the height to be set
 */
WinManager.center = function (iWidth,iHeight) {
    if (typeof(dialogArguments)=="undefined"){
        if (iWidth>screen.availWidth)
            iWidth=screen.availWidth;
        if (iHeight>screen.availHeight)
            iHeight=screen.availHeight;
        moveTo((screen.availWidth-iWidth)/2,(screen.availHeight-iHeight)/2);
        resizeTo(iWidth, iHeight);
    } else {
        if (iWidth>screen.availWidth)
            iWidth=screen.availWidth;
        if (iHeight>screen.availHeight)
            iHeight=screen.availHeight;
        
        dialogLeft=(screen.availWidth-iWidth)/2+"px";
        dialogWidth=iWidth+"px";
        dialogTop=(screen.availHeight-iHeight)/2+"px";
        dialogHeight=iHeight+"px";
    }
}

/**
 *  resize and right center the window
 *  @param iWidth the width to be set
 *  @param iHeight the height to be set
 */
WinManager.right = function (iWidth,iHeight) {
    if (typeof(dialogArguments)=="undefined"){
        if (iWidth>screen.availWidth)
            iWidth=screen.availWidth;
        if (iHeight>screen.availHeight)
            iHeight=screen.availHeight;
        moveTo((screen.availWidth-iWidth),(screen.availHeight-iHeight)/2);
        resizeTo(iWidth, iHeight);
    } else {
        if (iWidth>screen.availWidth)
            iWidth=screen.availWidth;
        if (iHeight>screen.availHeight)
            iHeight=screen.availHeight;
        dialogLeft=(screen.availWidth-iWidth)+"px";
        dialogWidth=iWidth+"px";
        dialogTop=(screen.availHeight-iHeight)/2+"px";
        dialogHeight=iHeight+"px";
    }
}


/**
 *  resize the window to screen's size
 */
WinManager.maximize = function () {
    if (typeof(dialogArguments)=="undefined"){
        moveTo(0,0);
        resizeTo(screen.availWidth, screen.availHeight);
    } else {
        dialogLeft="0px";
        dialogTop="0px";
        dialogWidth=screen.availWidth+"px";
        dialogHeight=screen.availHeight+"px";
    }
}

/**
 *  decode the parameters passed by parent window the parameters are decoded and set in
 *  current window context
 */
WinManager.decodeParams = function () {
    try {
    	var url = window.location.search.substr(1);
    	var strs = url.split("\"");
    	if(strs.length == 1) strs = url.split("%22");
    	var temp = "";
    	if(strs.length  == 3){
    	    url = strs[0] + "TEMPSTR" + strs[2];
            temp = "\"" + strs[1] + "\"";
        }
        var idx,a = url.split('&');
        for (var i = 0; i< a.length; i++) {
            idx = a[i].indexOf('=');
            var value = a[i].substr(idx+1);
            if(value == "TEMPSTR" && temp != "") value = temp;
            eval(a[i].substr(0,idx)+'='+unescape(unescape(value)));
        }
    } catch(e) {
        return;
    }
}

/**
 *  a very safe method to transfer small parameters to another window, but cann't be a complex structures
 */
WinManager.encodeParams = function (sURL,params) {
    //if( params instanceof Array){
    if( null != params && "undefined" != typeof(params.length)){
        if (params.length > 0) {
            sURL += "?" + params[0]+"="+params[1];

        }
        for (var i = 2; i<params.length; i+=2) {
            sURL += "&" + params[i]+"="+params[i+1];
        }
        return sURL;
    }
    else
    {
        return sURL;
    }
}

/**
 *  parse an Style string and return a Style object
 */
WinManager.parseStyle =  function (sStyle) {
    var aStyle = sStyle.split(";");
    var oStyle = new Object();
    var attstr, aCtrlAttrs = new Array();
    for (var i = 0; i< aStyle.length; i++) {
        attstr = aStyle[i].split(":");
        if (attstr[0] == "" || attstr[1] == null) continue;
        var an = attstr[0].trim().split("-");
        var n = an[0];
        for (var j=1 ; j<an.length; j++)
            n += an[j].charAt(0).toUpperCase()+an[j].substring(1);
        oStyle[n] = attstr[1].trim();
    }
    return oStyle;
}

/**
 *  parse a key string and return an Array
 *  every two element from the first element is a key couple: key-name and key-value
 */
WinManager.parseKey = function (sKey) {
    var aKey = sKey.split("&");
    var aRes = new Array(aKey.length*2);
    for (var j =0, i = 0; i< aKey.length; i++) {
        var attstr = aKey[i].split("=");
        if (attstr[0] == "") continue;
        aRes[j*2]= attstr[0].trim();
        aRes[j*2+1] = (attstr[1]!=null)? attstr[1]:"";
        j++;
    }
    aRes.length = j*2;
    return aRes;
}

/**
 *  parse a key string and return an Object
 */
WinManager.parseObject = function (sKey) {
    var aKey = sKey.split("&");
    var o = new Object();
    for (var j =0, i = 0; i< aKey.length; i++) {
        var attstr = aKey[i].split("=");
        if (attstr[0] == "") continue;
        o[attstr[0].trim()] =  (attstr[1]!=null)? attstr[1]:"";
        j++;
    }
    return o;
}

/**
 *  check whether the object is null or is a null string
 *  return true, if object is null or null string
 *  return false, else.
 */
WinManager.isEmpty = function (str) { return str == null || str == "";}

WinManager.nop = function() {}

//Add a new trim function to String object
var $G_RE_TRIM = /(^\s*)|(\s*$)/g;
String.prototype.trim = function () {
        return this.replace($G_RE_TRIM,"");
}
